package com.mls.guardian.appst.chain.excutor;

import com.mls.guardian.appst.chain.TaskExecutor;
import com.mls.guardian.appst.chain.TaskInvoker;
import com.mls.guardian.appst.chain.TaskResult;
import com.mls.guardian.common.FileUtil;
import com.mls.guardian.dataobj.ModuleMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;

/**
 * Created by pengrenfang on 15/11/13.
 * 更新静态文件js,css等
 * 下载源代码,复制静态文件到发布目录,替换js
 */
public class UpdateStaticFileExcutor implements TaskExecutor {
    private static final Logger log = LoggerFactory.getLogger(UpdateStaticFileExcutor.class);

    @Override
    public TaskResult exec(TaskInvoker invoker) {
        TaskResult result = new TaskResult();
        try {
            List<String> appList = invoker.getProject().getWebModuleList();
            List<String> aliasList = invoker.getProject().getWebAliasList();
            List<ModuleMessage> moduleList = invoker.getModuleList();

            if (moduleList != null && moduleList.size() > 0) {
                //替换指定模块
                for (ModuleMessage item : moduleList) {
                    int index = appList.indexOf(item.getModuleName());
                    log.info("----开始处理静态文件,模块名[{}]----", item.getModuleName());

                    String sourceFolder = invoker.getProject().getProjectPath() + File.separator + item.getModuleName() + File.separator + "src" + File.separator + "main" + File.separator + "webapp";
                    String targetFolder = invoker.getConfig().getMoveDest() + File.separator + invoker.getProject().getProjectName() + File.separator + aliasList.get(index);
                    if (item.getModuleFile() != null && item.getModuleFile().size() > 0) {
                        //更新指定文件
                        for (String fileItem : item.getModuleFile()) {
                            Path sourcePath = Paths.get(sourceFolder, fileItem);
                            Path targetPath = Paths.get(targetFolder, fileItem);
                            log.info("----复制文件[{}] -> [{}]----", sourcePath.toString(), targetPath.toString());
                            if (Files.exists(sourcePath) && false == Files.isDirectory(sourcePath)) {
                                Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
                            }
                        }
                    } else {
                        if (Files.exists(Paths.get(sourceFolder))) {
                            //正则.js结尾的
                            FileUtil.copyFolder(sourceFolder, targetFolder, ".+.js$", true);
                            FileUtil.copyFolder(sourceFolder, targetFolder, ".+.htm$", true);
                            FileUtil.copyFolder(sourceFolder, targetFolder, ".+.html$", true);
                            FileUtil.copyFolder(sourceFolder, targetFolder, ".+.css$", true);
                        }
                    }

                    log.info("----结束处理静态文件,模块名[{}]----", item.getModuleName());
                }
            } else {
                //全部替换
                for (int i = 0; i < appList.size(); i++) {
                    String item = appList.get(i);
                    log.info("----开始处理静态文件,模块名[{}]----", item);

                    String sourceFolder = invoker.getProject().getProjectPath() + File.separator + item + File.separator + "src" + File.separator + "main" + File.separator + "webapp";
                    String targetFolder = invoker.getConfig().getMoveDest() + File.separator + invoker.getProject().getProjectName() + File.separator + aliasList.get(i);

                    if (Files.exists(Paths.get(sourceFolder))) {
                        //正则.js结尾的
                        FileUtil.copyFolder(sourceFolder, targetFolder, ".+.js$", true);
                        FileUtil.copyFolder(sourceFolder, targetFolder, ".+.htm$", true);
                        FileUtil.copyFolder(sourceFolder, targetFolder, ".+.html$", true);
                        FileUtil.copyFolder(sourceFolder, targetFolder, ".+.css$", true);
                    }

                    log.info("----结束处理静态文件,模块名[{}]----", item);
                }
            }

            result.setSuc(true);
        } catch (IOException e) {
            result.setSuc(false);
            result.setError(e.getMessage());

            e.printStackTrace();
        } finally {
            log.info("----结束处理静态文件,模块名[{}]----", result.isSuc());
        }

        if (result.isSuc() == true) {
            return invoker.invoke();
        }

        return result;
    }
}
